<%@ page language="java" contentType="text/html; charset=utf-8"  import="com.brother.model.*" pageEncoding="utf-8"%>
<jsp:useBean id="carModelService" class="com.brother.service.CarModelService"></jsp:useBean>
<%
String modelname = request.getParameter("modelname");
String description = request.getParameter("description");
String description_zh = request.getParameter("description_zh");
String action = request.getParameter("action");
String carModelId =  request.getParameter("carModelId");
if(modelname != null && description != null){
	if(action.equals("edit")){
		
	}else{
		carModelService.addCarModel(modelname, description, description_zh);
	}
	response.sendRedirect("carmodels.jsp");
	return;
}


CarModel model = null;
if(carModelId != null){
	model = carModelService.getCarModel("carModelId",carModelId);
}

%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>add model</title>
<link href="../common.css" rel="stylesheet" type="text/css">
</head>
  <script type='text/javascript' src='../dwr/engine.js'> </script>
  <script type='text/javascript' src='../dwr/util.js'> </script>
  <script type='text/javascript' src='../dwr/interface/CarModelService.js'> </script>
<script>
var isExistsName = false;
function setText(){
	if(isExistsName && isEdit == false){
		alert('The model already exists!');
		document.getElementById("modelname").focus();
		return false;
	}
	var descFrame = window.frames["descriptionFrame"];
	var html = descFrame.document.documentElement.innerHTML;//descFrame.documentElement.innerHTML;
	document.getElementById("description").value=html;

	descFrame = window.frames["description_zhFrame"];
	html = descFrame.document.documentElement.innerHTML;//descFrame.documentElement.innerHTML;
	document.getElementById("description_zh").value=html;	
	return true;
}
function checkIsExists(input){
var name = input.value;
CarModelService.getCarModel("name",name, function(model){
	if(model != null){
		isExistsName = true;
	}else{
		isExistsName = false;
	}
});

}
var isEdit = false;
function initDesign(){
	var descFrame = window.frames["descriptionFrame"];
	descFrame.document.designMode = 'on';	
	var textarea = document.getElementById("description");
	if(textarea.value !=""){
		isEdit = true;
		descFrame.document.documentElement.innerHTML = textarea.value;
	}
	descFrame = window.frames["description_zhFrame"];
	descFrame.document.designMode = 'on';
	textarea = document.getElementById("description_zh");
	if(textarea.value !=""){
		descFrame.document.documentElement.innerHTML = textarea.value;
	}

}
</script>
<body onload="initDesign()">
<form name="form1" method="post" action="addModel.jsp" onSubmit="return setText();">
  <table width="800" border="0" cellpadding="5" cellspacing="1" bgcolor="#0066FF">
    <tr>
      <td width="126" bgcolor="#FFFFFF">Model Name : </td>
      <td width="674" bgcolor="#FFFFFF"><input name="modelname" id="modelname" onBlur="checkIsExists(this)" type="text" value="<%=model == null ?"":model.getName() %>" size="30" maxlength="50"></td>
    </tr>
    <tr>
      <td bgcolor="#FFFFFF">Description :</td>
      <td bgcolor="#FFFFFF"><iframe id="descriptionFrame" name="descriptionFrame" width="100%"  src="text.html" height="600" scrolling="auto" frameborder="1"></iframe></td>
    </tr>
    <tr>
      <td bgcolor="#FFFFFF">Description Chinese:</td>
      <td bgcolor="#FFFFFF"><iframe id="description_zhFrame" name="description_zhFrame" width="100%"  src="text.html" height="600" scrolling="auto" frameborder="1"></iframe></td>
    </tr>
    <tr>
      <td colspan="2" bgcolor="#FFFFFF"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td width="16%">&nbsp;<textarea style="display:none" name="description" id="description"><%=model == null ?"":model.getDescription()%></textarea>
          <textarea style="display:none" name="description_zh" id="description_zh"><%=model == null ?"":model.getDescription_zh()==null?"":model.getDescription_zh()%></textarea>
          </td>
          <td width="84%"><input type="hidden" name="action" value="<%=model==null?"add":"edit" %>"/>
          <input type="hidden" name="carModelId" value="<%=model==null?"":model.getCarmodelId() %>"/>
          <input type="submit" name="Submit" value="Submit"></td>
        </tr>
      </table></td>
    </tr>
  </table>
</form>
</body>
</html>
